package com.hww.hystrix.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Author: Wenwu Huang
 * @Date: 2018/10/10 16:16
 */
@RestController
@DefaultProperties(defaultFallback = "defaultFallback")
public class HystrixController {

    //当http://localhost:8083/getProduct不可访问的时候, 发生降级

    @HystrixCommand(fallbackMethod = "fallback")
//    @HystrixCommand
    @GetMapping("/getProductList")
    public String getProductList(){
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject("http://localhost:8083/getProduct", String.class);

        return result;
    }

    private String fallback(){
        return "请稍后再试~~~";
    }

    private String defaultFallback(){
        return "默认fallback,请稍后再试~~~";
    }

    @HystrixCommand(commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value="1000"),
            @HystrixProperty(name="circuitBreaker.enabled", value="true"), //设置熔断, 某个服务单元发生问题, 则断路
            @HystrixProperty(name="circuitBreaker.requestVolumeThreshold", value="10"),
            @HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds", value="10000"),
            @HystrixProperty(name="circuitBreaker.errorThresholdPercentage", value="60")
    })
    @GetMapping("/getProductList2")
    public String getProductList2(){
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject("http://localhost:8083/getProduct", String.class);

        return result;
    }
}
